import imgDefauleAvatar from '@/images/defauleAvatar.png';
import imgDefaultGroupAvatar from '@/images/defaultGroupAvatar.png';

export const handleAvatarError = (e: any) => {
  e.target.src = imgDefauleAvatar;
  e.target.onerror = null;
};

export const handleGroupAvatarError = (e: any) => {
  e.target.src = imgDefaultGroupAvatar;
  e.target.onerror = null;
};

export const handleAvatarErrorByType = (e: any, isgroup: boolean) => {
  e.target.src = isgroup ? imgDefaultGroupAvatar :  imgDefauleAvatar;
  e.target.onerror = null;
};

import Socket from './socket.js';

export function connectSocket(chatapiId: string, fns: any) {
  //    判断专家是否登录
  const socket = new Socket({
    socketUrl: `ws://146.196.52.224:8080/chatenabler/websocket/${chatapiId}`,
    timeout: 5000,
    socketMessage: (receive: any) => {
      fns.receive && fns.receive(receive);
    },
    socketClose: (msg: any) => {
      console.log(msg);
    },
    socketError: () => {
      console.log('连接建立失败');
      fns.fail && fns.fail();
    },
    socketOpen: () => {
      console.log('连接建立成功');
      fns.success && fns.success();
    },
  });
  try {
    socket.connection();
  } catch (e) {}
  return socket;
}

export function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}

export function isImage(type: string) {
  return ['png', 'jpg', 'jpeg', 'gif', 'image'].includes(type);
}

// 生成uuid
export function guid2() {
  function S4() {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}


export function copyFn(text: string) {
  var txtEl = document.createElement('textarea');
  txtEl.value = text;
  document.body.appendChild(txtEl);
  txtEl.select(); // 选择对象
  document.execCommand("Copy"); // 执行浏览器复制命令
  txtEl.remove();
}